<template>
	<view>
		<scroll-view id="scroll" scroll-y="true"
		 :scroll-top="scrolltop" 
		 :scroll-with-animation="true"
		  :style="{height:style.contenH+'px'}"
		  >
			<block v-for="(item,index) in chatdata" :key="index">
				<chat-list :item="item"></chat-list>
			</block>
			
		</scroll-view>
		
	
		<!-- 底部 -->
			<chat-input @submit="submit" :text="text"></chat-input>
	</view>
</template>

<script>
	
	import chatInput from '../../components/chat/chat-input.vue'
	import chatList from '../../components/chat/chat-list.vue'
	import time from '../../common/time.js'
	export default {
		components:{
			chatInput,
			chatList
		},
		data() {
			return {
				text:'',
				scrolltop:0,//高度
				style:{
					contenH:0,
					itemH:0
				},
				chatdata:[{
					userimg:'../../static/demo/demo6.jpg',
					text:'你好？',
					isme:false,
					type:'text',
					img:'',
					time:'1593522036',
					timetext:''
				},
				{userimg:'../../static/demo/demo6.jpg',
					text:'你好',
					isme:true,
					type:'text',
					img:'',
					time:'1585573236',
					timetext:''
				},
				{userimg:'../../static/demo/demo6.jpg',
					text:'',
					isme:false,
					type:'img',
					img:'../../static/demo/datapic/1.jpg',
					time:'1601470422',
					timetext:''
				},
				{userimg:'../../static/demo/demo6.jpg',
					text:'好的11',
					isme:true,
					type:'text',
					img:'',
					time:'1601414854',
					timetext:''
				},
				]
			}
		},
		onLoad() {
		this.initdata()
		this.init()
		},
		onReady() {
			this.onsubmit()
		},
		methods: {
			init(){
			
				try {
				    const res = uni.getSystemInfoSync();
					this.style.contenH=res.windowHeight-uni.upx2px(120)
				    console.log(res.windowHeight);
				
				}catch (e){
					
				}
			},
			//提交时执行方法
			onsubmit(){
				let q = uni.createSelectorQuery();
				q.select("#scroll").boundingClientRect()
				q.selectAll(".chat-body").boundingClientRect()
				q.exec((res)=>{
					for (var i = 0; i < res[1].length; i++) {
						
						this.style.itemH+=res[1][i].height
					}
					if(this.style.itemH>this.style.contenH){
						this.scrolltop=this.style.itemH
					}
				})
				console.log(this.style.itemH)
				console.log(this.scrolltop)
			},
			//初始化数据
			initdata(){
				let arr=this.chatdata
				for (var i = 0; i < arr.length; i++) {
					arr[i].timetext=time.gettime.getChatTime(arr[i].time,i>0?arr[i-1].time:
					0)
					
				}
				this.chatdata=arr
						
			},
			//发送按钮
			submit(text){
				let obj={
					userimg:'../../static/demo/demo6.jpg',
						text:'好的',
						isme:true,
						type:'text',
						img:'',
						time:new Date().getTime(),
						timetext:time.gettime.getChatTime(time,this.chatdata[this.chatdata.length-1].time)
				}
				
				
				obj.timetext=time.gettime.getChatTime(obj.time,this.chatdata[this.chatdata.length-1].time)
				
			
				this.chatdata.push(obj)
				this.onsubmit()
			}
		}
	}
</script>

<style>

	
</style>
